package com.gency.subscribe.web.controller.system;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.baomidou.mybatisplus.plugins.Page;
import com.gency.subscribe.core.util.system.OSSClientUtils;
import com.gency.subscribe.model.system.CooperationInfo;
import com.gency.subscribe.model.system.PageBean;
import com.gency.subscribe.service.system.ICooperationInfoService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 马行空
 * @since 2018-06-19
 */
@Controller
@RequestMapping("/cooperation_info")
public class CooperationInfoController {

	@Autowired
	ICooperationInfoService cooperationInfoService;
	
	@GetMapping("/list")
	public ModelAndView list(PageBean pb,ModelAndView mv){
		Page<CooperationInfo> page = new Page<CooperationInfo>(pb.getCurPage(),pb.getPageSize());
		page.setAsc(false);
		page.setOrderByField("create_time");
		page = cooperationInfoService.selectPage(page);
		List<CooperationInfo> list = page.getRecords();
		pb = new PageBean(page.getTotal(), page.getPages(), page.getCurrent(), 10, page.getOffsetCurrent(), page.getOffset(),0);
		mv.addObject("pagebean",pb);
		mv.addObject("cooperationlist", list);
		mv.setViewName("cooperation/list");
		return mv;
	}
	
	
	@GetMapping("/add")
	public ModelAndView add(ModelAndView mv){
		mv.setViewName("cooperation/info");
		mv.addObject("cooperationInfo", new CooperationInfo());
		return mv;
	}
	
	@PostMapping("/add")
	public ModelAndView add(ModelAndView mv,CooperationInfo cooperationInfo,MultipartFile multipartFile){
		mv.setViewName("redirect:list");
		String url ="";
		try {
			if(multipartFile != null && !multipartFile.isEmpty()){
				url = OSSClientUtils.URL_PREFIX+OSSClientUtils.putOSSObject(multipartFile);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		cooperationInfo.setLogo_url(url);
		cooperationInfoService.insert(cooperationInfo);
		return mv;
	}
	
	
	@GetMapping("/update")
	public ModelAndView update(ModelAndView mv,Long id){
		mv.setViewName("cooperation/info");
		mv.addObject("cooperationInfo",cooperationInfoService.selectById(id));
		return mv;
	}
	
	@PostMapping("/update")
	public ModelAndView update(ModelAndView mv ,CooperationInfo cooperationInfo,MultipartFile multipartFile){
		mv.setViewName("redirect:list");
		String url ="";
		try {
			if(multipartFile != null && !multipartFile.isEmpty()){
				url = OSSClientUtils.URL_PREFIX+OSSClientUtils.putOSSObject(multipartFile);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(!url.equals("")){
			cooperationInfo.setLogo_url(url);
		}
		cooperationInfoService.updateById(cooperationInfo);
		return mv;
	}
	
	@GetMapping("/delete")
	public ModelAndView add(ModelAndView mv,Long id){
		mv.setViewName("redirect:list");
		cooperationInfoService.deleteById(id);
		return mv;
	}
}

